<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>绿能充 - 个人中心</title>
    <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        :root {
            --primary-color: #00a896;
            --secondary-color: #02c39a;
            --dark-color: #05668d;
            --light-color: #f0f3bd;
            --accent-color: #028090;
            --text-dark: #333;
            --text-light: #f8f9fa;
            --shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
            --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Noto Sans SC', sans-serif;
            color: var(--text-dark);
            background-color: #f9f9f9;
            min-height: 100vh;
            display: flex;
            overflow-x: hidden;
        }

        /* 侧边栏样式 - 固定定位 */
        .sidebar {
            width: 250px;
            background: linear-gradient(135deg, var(--dark-color) 0%, var(--primary-color) 100%);
            color: white;
            padding: 20px 0;
            height: 100vh;
            position: fixed;
            box-shadow: var(--shadow);
            z-index: 100;
            overflow-y: auto;
        }

        .sidebar-header {
            padding: 0 20px 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 20px;
        }

        .sidebar-header .logo {
            display: flex;
            align-items: center;
            margin-bottom: 15px;
        }

        .sidebar-header .logo-icon {
            width: 40px;
            height: 40px;
            background-color: rgba(255, 255, 255, 0.2);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            font-size: 18px;
            color: var(--light-color);
        }

        .sidebar-header h3 {
            font-size: 1.2rem;
            font-weight: 500;
            margin: 0;
        }

        .sidebar-header p {
            font-size: 0.8rem;
            opacity: 0.8;
            margin: 0;
        }

        .nav-menu {
            list-style: none;
            padding: 0 15px;
        }

        .nav-item {
            margin-bottom: 5px;
        }

        .nav-link {
            display: flex;
            align-items: center;
            padding: 12px 15px;
            color: rgba(255, 255, 255, 0.8);
            text-decoration: none;
            border-radius: 8px;
            transition: var(--transition);
        }

        .nav-link:hover, .nav-link.active {
            background-color: rgba(255, 255, 255, 0.1);
            color: white;
        }

        .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
            font-size: 16px;
        }

        .nav-link .badge {
            margin-left: auto;
            background-color: var(--light-color);
            color: var(--dark-color);
            font-size: 0.7rem;
            padding: 3px 6px;
            border-radius: 10px;
        }

        .user-profile {
            position: absolute;
            bottom: 20px;
            left: 0;
            width: 100%;
            padding: 0 20px;
        }

        .user-info {
            display: flex;
            align-items: center;
            padding: 10px;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            cursor: pointer;
            transition: var(--transition);
        }

        .user-info:hover {
            background-color: rgba(255, 255, 255, 0.2);
        }

        .user-avatar {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, 0.2);
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
            font-size: 14px;
        }

        .user-name {
            font-size: 0.9rem;
            font-weight: 500;
            margin-bottom: 2px;
        }

        .user-role {
            font-size: 0.7rem;
            opacity: 0.8;
        }

        /* 主内容区样式 */
        .main-content {
            flex: 1;
            margin-left: 250px;
            padding: 30px;
            min-height: 100vh;
            background-color: #f5f7fa;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            background-color: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: var(--shadow);
        }

        .page-title h1 {
            font-size: 1.8rem;
            font-weight: 600;
            color: var(--dark-color);
            margin-bottom: 5px;
        }

        .page-title p {
            color: #777;
            font-size: 0.9rem;
            margin: 0;
        }

        .header-actions {
            display: flex;
            gap: 10px;
        }

        .btn {
            padding: 8px 16px;
            border-radius: 8px;
            font-size: 0.9rem;
            font-weight: 500;
            border: none;
            cursor: pointer;
            transition: var(--transition);
            display: flex;
            align-items: center;
        }

        .btn i {
            margin-right: 6px;
        }

        .btn-primary {
            background: linear-gradient(to right, var(--primary-color), var(--secondary-color));
            color: white;
            box-shadow: 0 4px 15px rgba(0, 168, 150, 0.3);
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 20px rgba(0, 168, 150, 0.4);
        }

        .btn-outline {
            background: transparent;
            border: 1px solid #ddd;
            color: #555;
        }

        .btn-outline:hover {
            background-color: #f5f5f5;
        }

        /* 卡片样式 */
        .card {
            background-color: white;
            border-radius: 12px;
            box-shadow: var(--shadow);
            padding: 20px;
            margin-bottom: 20px;
            transition: var(--transition);
        }

        .card:hover {
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
        }

        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }

        .card-header h3 {
            font-size: 1.2rem;
            font-weight: 600;
            margin: 0;
            color: var(--dark-color);
        }

        /* 内容行布局 */
        .content-row {
            display: grid;
            grid-template-columns: 2fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }

        @media (max-width: 1200px) {
            .content-row {
                grid-template-columns: 1fr;
            }
        }

        /* 用户信息样式 */
        .user-profile-info {
            padding: 10px;
        }

        .avatar-section {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            cursor: pointer;
            transition: var(--transition);
        }

        .avatar-section:hover {
            opacity: 0.9;
        }

        .avatar-img {
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background-color: #f0f3bd;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 20px;
            overflow: hidden;
        }

        .avatar-img img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .avatar-img i {
            font-size: 36px;
            color: #05668d;
        }

        .info-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
        }

        .info-item {
            margin-bottom: 15px;
        }

        .info-item label {
            display: block;
            font-size: 0.9rem;
            color: #777;
            margin-bottom: 5px;
        }

        .info-item p {
            font-size: 1rem;
            margin: 0;
            padding: 8px 12px;
            background-color: #f9f9f9;
            border-radius: 6px;
        }

        /* 表格样式 */
        .table-responsive {
            overflow-x: auto;
            border-radius: 8px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
        }

        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }

        th {
            font-weight: 500;
            color: #555;
            font-size: 0.9rem;
            background-color: #f9f9f9;
        }

        tr:hover {
            background-color: #f5f7fa;
        }

        /* 徽章样式 */
        .badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.75rem;
            font-weight: 500;
        }

        .badge-success {
            background-color: #e8f5e9;
            color: #4caf50;
        }

        .badge-primary {
            background-color: #e3f2fd;
            color: #2196f3;
        }

        .badge-warning {
            background-color: #fff8e1;
            color: #ff9800;
        }

        .badge-danger {
            background-color: #ffebee;
            color: #f44336;
        }

        .badge-secondary {
            background-color: #e0e0e0;
            color: #616161;
        }

        /* 统计卡片样式 */
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 20px;
            padding: 20px 0;
        }

        .stat-item {
            text-align: center;
            padding: 15px;
            background-color: white;
            border-radius: 8px;
            box-shadow: var(--shadow);
        }

        .stat-label {
            font-size: 1rem;
            color: #777;
            margin-bottom: 10px;
        }

        .stat-value {
            font-size: 1.8rem;
            font-weight: 600;
            color: var(--dark-color);
        }

        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
                overflow: hidden;
            }

            .sidebar-header h3,
            .sidebar-header p,
            .nav-link span,
            .user-name,
            .user-role {
                display: none;
            }

            .sidebar-header {
                padding: 15px 10px;
            }

            .nav-link {
                justify-content: center;
                padding: 12px 0;
            }

            .nav-link i {
                margin-right: 0;
                font-size: 18px;
            }

            .main-content {
                margin-left: 70px;
            }
        }

        @media (max-width: 768px) {
            .stats-grid {
                grid-template-columns: 1fr 1fr;
            }

            .header {
                flex-direction: column;
                align-items: flex-start;
                gap: 15px;
            }
        }

        @media (max-width: 576px) {
            .stats-grid {
                grid-template-columns: 1fr;
            }

            .main-content {
                padding: 20px 15px;
            }

            .info-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<!-- 侧边导航栏 - 固定定位 -->
<div class="sidebar">
    <div class="sidebar-header">
        <div class="logo">
            <div class="logo-icon">
                <i class="fas fa-charging-station"></i>
            </div>
            <div>
                <h3>绿能充</h3>
                <p>用户面板</p>
            </div>
        </div>
    </div>

    <ul class="nav-menu">
        <li class="nav-item">
            <a href="/user/home" class="nav-link">
                <i class="fas fa-tachometer-alt"></i>
                <span>仪表盘</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/user/announcement/list" class="nav-link">
                <i class="fas fa-bullhorn"></i>
                <span>公告栏</span>
                <span class="badge">5</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/user/pile/list" class="nav-link">
                <i class="fas fa-charging-station"></i>
                <span>充电桩</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/user/map/mapDetail" class="nav-link">
                <i class="fas fa-map-marked-alt"></i>
                <span>地图</span>
            </a>
        </li>
        <li class="nav-item">
            <a href="/user/center/personCenter/${user.id}" class="nav-link active">
                <i class="fas fa-user-circle"></i>
                <span>个人中心</span>
                <span class="badge">3</span>
            </a>
        </li>
    </ul>

    <div class="user-profile">
        <div class="user-info">
            <div class="user-avatar">
                <i class="fas fa-user"></i>
            </div>
            <div>
                <div class="user-name">${user.username}</div>
                <div class="user-role">
                    <c:choose>
                        <c:when test="${user.role == 1}">管理员</c:when>
                        <c:when test="${user.role == 2}">测试员</c:when>
                        <c:otherwise>普通用户</c:otherwise>
                    </c:choose>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 主要内容区 -->
<div class="main-content">
    <div class="header">
        <div class="page-title">
            <h1>个人中心</h1>
            <p>管理您的账户信息和设置</p>
        </div>
        <div class="header-actions">
            <button class="btn btn-outline" onclick="location.href='/user/center/edit'">
                <i class="fas fa-edit"></i> 编辑资料
            </button>
            <button class="btn btn-outline" onclick="location.href='/user/center/security'">
                <i class="fas fa-lock"></i> 安全设置
            </button>
        </div>
    </div>

    <!-- 用户信息卡片 -->
    <div class="content-row">
        <!-- 基本信息 -->
        <div class="card">
            <div class="card-header">
                <h3>基本信息</h3>
            </div>
            <div class="user-profile-info">
                <div class="avatar-section" onclick="document.getElementById('avatarUpload').click()">
                    <div class="avatar-img">
                        <c:choose>
                            <c:when test="${not empty user.avatar}">
                                <img src="${user.avatar}" alt="用户头像">
                            </c:when>
                            <c:otherwise>
                                <i class="fas fa-user"></i>
                            </c:otherwise>
                        </c:choose>
                    </div>
                    <div>
                        <h2 style="margin: 0 0 5px 0;">
                            <c:out value="${not empty user.realName ? user.realName : '未设置'}" />
                        </h2>
                        <p style="margin: 0; color: #777;">@${user.username}</p>
                        <p style="margin: 5px 0 0 0; color: #777;">
                            <c:choose>
                                <c:when test="${user.role == 1}">管理员</c:when>
                                <c:when test="${user.role == 2}">测试员</c:when>
                                <c:otherwise>普通用户</c:otherwise>
                            </c:choose>
                        </p>
                    </div>
                    <input type="file" id="avatarUpload" accept="image/*" style="display: none;">
                </div>

                <div class="info-grid">
                    <div class="info-item">
                        <label>用户ID</label>
                        <p>${user.id}</p>
                    </div>
                    <div class="info-item">
                        <label>注册时间</label>
                        <p><fmt:formatDate value="${user.createTime}" pattern="yyyy-MM-dd HH:mm" /></p>
                    </div>
                    <div class="info-item">
                        <label>手机号码</label>
                        <p><c:out value="${not empty user.phone ? user.phone : '未设置'}" /></p>
                    </div>
                    <div class="info-item">
                        <label>电子邮箱</label>
                        <p><c:out value="${not empty user.email ? user.email : '未设置'}" /></p>
                    </div>
                    <div class="info-item">
                        <label>身份证号</label>
                        <p><c:out value="${not empty user.idCard ? user.idCard : '未设置'}" /></p>
                    </div>
                    <div class="info-item">
                        <label>最后登录</label>
                        <p>
                            <c:choose>
                                <c:when test="${not empty user.lastLoginTime}">
                                    <fmt:formatDate value="${user.lastLoginTime}" pattern="yyyy-MM-dd HH:mm" />
                                </c:when>
                                <c:otherwise>从未登录</c:otherwise>
                            </c:choose>
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 账户余额 -->
        <div class="card">
            <div class="card-header">
                <h3>账户余额</h3>
                <button class="btn btn-primary" >
                    <i class="fas fa-plus"></i> <a href="${pageContext.request.contextPath}/user/balance/add">充值</a>
                </button>
            </div>
            <div style="text-align: center; padding: 30px 0;">
                <div style="font-size: 2.5rem; font-weight: 600; color: var(--dark-color); margin-bottom: 10px;">
                    ¥<fmt:formatNumber value="${user.balance}" pattern="#,##0.00" />
                </div>
                <p style="color: #777;">可用余额</p>
                <div style="margin-top: 30px;">
                    <button class="btn btn-outline" onclick="location.href='/user/center/transactions'">
                        <i class="fas fa-list"></i> 查看交易记录
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 充电统计 -->
    <div class="card">
        <div class="card-header">
            <h3>充电统计</h3>
            <div>
                <button class="btn btn-outline btn-sm">本月</button>
                <button class="btn btn-outline btn-sm active">全部</button>
            </div>
        </div>
        <div class="stats-grid">
            <div class="stat-item">
                <div class="stat-label">总充电次数</div>
                <div class="stat-value">
                    <c:out value="${not empty totalChargingCount ? totalChargingCount : 0}" />
                </div>
            </div>
            <div class="stat-item">
                <div class="stat-label">总充电时长</div>
                <div class="stat-value">
                    <c:out value="${not empty totalChargingHours ? totalChargingHours : 0}" /> 小时
                </div>
            </div>
            <div class="stat-item">
                <div class="stat-label">总充电量</div>
                <div class="stat-value">
                    <fmt:formatNumber value="${not empty totalChargingEnergy ? totalChargingEnergy : 0}" pattern="#,##0.0" /> kWh
                </div>
            </div>
            <div class="stat-item">
                <div class="stat-label">总消费金额</div>
                <div class="stat-value">
                    ¥<fmt:formatNumber value="${not empty totalChargingAmount ? totalChargingAmount : 0}" pattern="#,##0.00" />
                </div>
            </div>
        </div>
    </div>

    <!-- 最近充电记录 -->
    <div class="card">
        <div class="card-header">
            <h3>最近充电记录</h3>
            <a href="/user/record/recordDetail" class="btn btn-outline">查看全部</a>
        </div>
        <div class="table-responsive">
            <table>
                <thead>
                <tr>
                    <th>订单ID</th>
                    <th>充电桩</th>
                    <th>开始时间</th>
                    <th>结束时间</th>
                    <th>充电量</th>
                    <th>金额</th>
                    <th>状态</th>
                </tr>
                </thead>
                <tbody>
                <c:choose>
                    <c:when test="${not empty recentRecords}">
                        <c:forEach items="${recentRecords}" var="record">
                            <tr>
                                <td>${record.id}</td>
                                <td>${record.pileId}</td>
                                <td><fmt:formatDate value="${record.startTime}" pattern="MM-dd HH:mm" /></td>
                                <td>
                                    <c:choose>
                                        <c:when test="${not empty record.endTime}">
                                            <fmt:formatDate value="${record.endTime}" pattern="MM-dd HH:mm" />
                                        </c:when>
                                        <c:otherwise>-</c:otherwise>
                                    </c:choose>
                                </td>
                                <td><fmt:formatNumber value="${record.energy}" pattern="#,##0.0" /> kWh</td>
                                <td>¥<fmt:formatNumber value="${record.amount}" pattern="#,##0.00" /></td>
                                <td>
                                    <c:choose>
                                        <c:when test="${record.status == 'COMPLETED'}">
                                            <span class="badge badge-success">已完成</span>
                                        </c:when>
                                        <c:when test="${record.status == 'CHARGING'}">
                                            <span class="badge badge-primary">充电中</span>
                                        </c:when>
                                        <c:when test="${record.status == 'CANCELLED'}">
                                            <span class="badge badge-warning">已取消</span>
                                        </c:when>
                                        <c:when test="${record.status == 'FAULT'}">
                                            <span class="badge badge-danger">故障中断</span>
                                        </c:when>
                                        <c:otherwise>
                                            <span class="badge badge-secondary">未知状态</span>
                                        </c:otherwise>
                                    </c:choose>
                                </td>
                            </tr>
                        </c:forEach>
                    </c:when>
                    <c:otherwise>
                        <tr>
                            <td colspan="7" style="text-align: center; padding: 30px;">暂无充电记录</td>
                        </tr>
                    </c:otherwise>
                </c:choose>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 头像上传功能
        document.getElementById('avatarUpload').addEventListener('change', function(e) {
            if (this.files && this.files[0]) {
                const file = this.files[0];
                if (!file.type.match('image.*')) {
                    alert('请选择图片文件');
                    return;
                }

                // 这里可以添加文件大小验证
                if (file.size > 2 * 1024 * 1024) {
                    alert('图片大小不能超过2MB');
                    return;
                }

                // 预览图片
                const reader = new FileReader();
                reader.onload = function(e) {
                    const avatarImg = document.querySelector('.avatar-img');
                    avatarImg.innerHTML = `<img src="\${e.target.result}" alt="用户头像">`;

                    // 这里可以添加AJAX上传逻辑
                    // uploadAvatar(file);
                };
                reader.readAsDataURL(file);
            }
        });

        // 侧边栏当前菜单高亮
        const currentPath = window.location.pathname;
        document.querySelectorAll('.nav-link').forEach(link => {
            if (link.getAttribute('href') === currentPath) {
                link.classList.add('active');
            } else {
                link.classList.remove('active');
            }
        });
    });

    // 实际的上传函数
    function uploadAvatar(file) {
        const formData = new FormData();
        formData.append('avatar', file);

        fetch('/user/center/uploadAvatar', {
            method: 'POST',
            body: formData
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('头像更新成功');
                    // 可以刷新页面或更新头像URL
                } else {
                    alert('头像上传失败: ' + data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('上传过程中出错');
            });
    }
</script>
</body>
</html>